package ha.web.client.servlet.runGame;

import ha.web.server.exceptions.RegisterException;
import ha.web.server.exceptions.UserNotFoundException;
import ha.web.server.manager.GameMangerServerSide;

import java.io.IOException;
import java.io.PrintWriter;

import ha.web.server.exceptions.LoginException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WaitingPage
 */
@WebServlet("/WaitingPage")
public class WaitingPage extends HttpServlet {
       
    /**
	 * 
	 */
	private static final long serialVersionUID = -5808687622681761865L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public WaitingPage() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    	chkCookieEnable(request,response);
		PrintWriter out = response.getWriter();
		GameMangerServerSide ins = GameMangerServerSide.getInstance();
		String userName = request.getParameter("user");
		String userPass = request.getParameter("pass");
		int userId = -1;
		String nickName  = null;
		try {
			userId = ins.playerLogin(userName, userPass);
			nickName = ins.getNickNameById(userId);
		} catch (UserNotFoundException | RegisterException | LoginException e) {
			out.print(e.getMessage());
			ServletContext context = getServletContext();
			RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
			dispatcher.forward(request, response);
			return;
		}
		Cookie userCookie = new Cookie("chinese_checkers", nickName+":"+String.valueOf(userId));
		userCookie.setComment(ins.getComm());
		response.addCookie(userCookie);
		
		int gameId;
		try {
			gameId = ins.getGameIdByUserId(userId);
		} catch (UserNotFoundException e) {
			gameId = -1;
		}
		//check if user is admin - if so go to admin page
		 if (ins.isUserAdmin(userId)){
			out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
			out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
			out.print("<script type=\"text/javascript\">window.location.href = \"AdminPage\"</script>)");
			out.print("</HEAD></HTML>");	
		}
		
		//check if user have a game - if so - load it if no - check waiting list
		if (gameId>0) {
			out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
			out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
			out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
			out.print("<h2> Welcome " + nickName + "<h2>");
			out.print("<h2>The game will start immediately<br><h2>");
			//start function to check if current player turn:
			out.print("<script src=\"in_game_check.js\" type=\"text/javascript\"></script>");
			out.print("<script>short_wait()</script>");
			out.print("</div>");
			out.print("</BODY></HTML>");
		}else{
			out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
			out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
			out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
			out.print("<h2> Welcome " + nickName + "<h2>");
			out.print("<h2>waiting for other player to log in,<br> please be patient<h2>");
			out.print("<div id = \"debug\"></div>");
			//start function to check if current player turn:
			out.print("<script src=\"in_game_check.js\" type=\"text/javascript\"></script>");
			out.print("<script>check_if_in_game()</script>");
			out.print("</div>");
			out.print("</BODY></HTML>");
		}	
	}

	private void chkCookieEnable(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
		out.print("<HTML><HEAD><TITLE>Cookie Test Page</TITLE>");	
		out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
		
		out.print("<script src=\"cookiesEnabledChk.js\" type=\"text/javascript\"></script>");
		out.print("<script>areCookiesEnabled()</script>");
		out.print("</div>");
		out.print("</BODY></HTML>");
		
	}
}

		

